package com.ocm.tourism.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ocm.tourism.core.domain.Comment;
import com.ocm.tourism.core.domain.Participant;
import com.ocm.tourism.core.service.CommentService;
import com.ocm.tourism.core.service.ParticipantService;
import com.ocm.tourism.core.util.UserUtil;

@RequestMapping("/comment")
@Controller
public class CommentController {

   @Autowired
   CommentService commentService;

   @Autowired
   ParticipantService participantService;

   @RequestMapping(value = "/{attractionType}/{attractionMenu}", method=RequestMethod.GET)
   public String viewAttractionComments(@PathVariable("attractionType") String attractionType,@PathVariable("attractionMenu") String attractionMenu,
         @RequestParam(value = "page", required = false) Integer page,
         @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
      uiModel.addAttribute("attractionType", attractionType);
      uiModel.addAttribute("attractionMenu", attractionMenu);
      String cateloguesType = createCateloguesType(attractionType, attractionMenu);
      long count = commentService.count(cateloguesType);
      if (page != null || size != null) {
          int sizeNo = size == null ? 10 : size.intValue();
          final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
          uiModel.addAttribute("comments", commentService.findComments(cateloguesType,firstResult, sizeNo));
          float nrOfPages = (float) count / sizeNo;
          uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
      } else {
          List<Comment> comments =  count == 0 ? new ArrayList<Comment>() : commentService.findComments(cateloguesType,0, 25);
          uiModel.addAttribute("comments", comments);
      }
      return "comment/list";
   }

   @RequestMapping(value = "/catalogue/{catalogueId}", method=RequestMethod.GET)
   public String viewCatalogueComments(@PathVariable("catalogueId") long catalogueId,
         @RequestParam(value = "page", required = false) Integer page,
         @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
      long count = commentService.countByCatalogue(catalogueId);
      if (page != null || size != null) {
          int sizeNo = size == null ? 10 : size.intValue();
          final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
          uiModel.addAttribute("comments", commentService.findCommentsByCatalogue(catalogueId,firstResult, sizeNo));
          float nrOfPages = (float) count / sizeNo;
          uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
      } else {
          List<Comment> comments =  count == 0 ? new ArrayList<Comment>() : commentService.findCommentsByCatalogue(catalogueId,0, 25);
          uiModel.addAttribute("comments", comments);
      }
      uiModel.addAttribute("catalogueId", catalogueId);
      return "comment/catalogue/list";
   }
   
   @RequestMapping(method=RequestMethod.POST,produces = "text/html")
   public String createComment(@Valid Comment comment, BindingResult bindingResult,Model uiModel,  HttpServletRequest httpServletRequest) throws Exception {
      if (bindingResult.hasErrors()){
         uiModel.addAttribute("status", "FAIL");
         return "comment/status";
      }
      comment.setBusinessId(null);
      comment.setCreatedDate(new Date(System.currentTimeMillis()));
      if (UserUtil.hasLogined()){
         comment.setCreatedUser(UserUtil.getAuthenticated().getUsername());
         Participant participant = this.participantService.findParticipant(UserUtil.getAuthenticated().getParticipantId());
         comment.setParticipant(participant);
      }
      commentService.save(comment);
      uiModel.addAttribute("status", "SUCC");
      return "comment/status";
   }

   @RequestMapping(value="/business/{businessId}", method=RequestMethod.POST,produces = "text/html")
   public String createBusinessComment(@PathVariable("businessId") long businessId, @Valid Comment comment, BindingResult bindingResult,Model uiModel,  HttpServletRequest httpServletRequest) throws Exception {
      if (bindingResult.hasErrors()){
         uiModel.addAttribute("status", "FAIL");
         return "comment/status";
      }
      comment.setId(null);
      comment.setBusinessId(businessId);
      comment.setCreatedDate(new Date(System.currentTimeMillis()));
      if (UserUtil.hasLogined()){
         comment.setCreatedUser(UserUtil.getAuthenticated().getUsername());
         Participant participant = this.participantService.findParticipant(UserUtil.getAuthenticated().getParticipantId());
         comment.setParticipant(participant);
      }
      commentService.saveBusinessComment(businessId, comment);
      uiModel.addAttribute("status", "SUCC");
      return "comment/status";
   }
   
   @RequestMapping(value="/catalogue/{catalogueId}", method=RequestMethod.POST,produces = "text/html")
   public String createCatalogueComment(@PathVariable("catalogueId") long catalogueId, @Valid Comment comment, BindingResult bindingResult,Model uiModel,  HttpServletRequest httpServletRequest) throws Exception {
      if (bindingResult.hasErrors()){
         uiModel.addAttribute("status", "FAIL");
         return "comment/status";
      }
      comment.setId(null);
      comment.setCatalogueId(catalogueId);
      comment.setCreatedDate(new Date(System.currentTimeMillis()));
      if (UserUtil.hasLogined()){
         comment.setCreatedUser(UserUtil.getAuthenticated().getUsername());
         Participant participant = this.participantService.findParticipant(UserUtil.getAuthenticated().getParticipantId());
         comment.setParticipant(participant);
      }
      commentService.save(comment);
      uiModel.addAttribute("status", "SUCC");
      return "comment/status";
   }
   
   @RequestMapping(value = "/tour/{tourId}", method=RequestMethod.GET)
   public String viewTourComments(@PathVariable("tourId") long tourId,
         @RequestParam(value = "page", required = false) Integer page,
         @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
      long count = commentService.countByTour(tourId);
      if (page != null || size != null) {
          int sizeNo = size == null ? 10 : size.intValue();
          final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
          uiModel.addAttribute("comments", commentService.findCommentsByTour(tourId,firstResult, sizeNo));
          float nrOfPages = (float) count / sizeNo;
          uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
      } else {
          List<Comment> comments =  count == 0 ? new ArrayList<Comment>() : commentService.findCommentsByTour(tourId,0, 25);
          uiModel.addAttribute("comments", comments);
      }
      uiModel.addAttribute("tourId", tourId);
      return "comment/tour/list";
   }
   
   @RequestMapping(value="/tour/{tourId}", method=RequestMethod.POST,produces = "text/html")
   public String createTourComment(@PathVariable("tourId") long tourId, @Valid Comment comment, BindingResult bindingResult,Model uiModel,  HttpServletRequest httpServletRequest) throws Exception {
      if (bindingResult.hasErrors()){
         uiModel.addAttribute("status", "FAIL");
         return "comment/status";
      }
      comment.setId(null);
      comment.setTourId(tourId);
      comment.setCreatedDate(new Date(System.currentTimeMillis()));
      if (UserUtil.hasLogined()){
         comment.setCreatedUser(UserUtil.getAuthenticated().getUsername());
         Participant participant = this.participantService.findParticipant(UserUtil.getAuthenticated().getParticipantId());
         comment.setParticipant(participant);
      }
      commentService.save(comment);
      uiModel.addAttribute("status", "SUCC");
      return "comment/status";
   }

   @RequestMapping(value = "/tourSale/{tourId}/{saleId}", method=RequestMethod.GET)
   public String viewTourSaleComments(@PathVariable("tourId") long tourId,@PathVariable("saleId") long saleId,
         @RequestParam(value = "page", required = false) Integer page,
         @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
      long count = commentService.countByTourSale(saleId);
      if (page != null || size != null) {
          int sizeNo = size == null ? 10 : size.intValue();
          final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
          uiModel.addAttribute("comments", commentService.findCommentsByTourSale(saleId,firstResult, sizeNo));
          float nrOfPages = (float) count / sizeNo;
          uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
      } else {
          List<Comment> comments =  count == 0 ? new ArrayList<Comment>() : commentService.findCommentsByTourSale(saleId,0, 25);
          uiModel.addAttribute("comments", comments);
      }
      uiModel.addAttribute("tourId", tourId);
      uiModel.addAttribute("saleId", saleId);
      return "comment/toursale/list";
   }
   
   @RequestMapping(value="/tourSale/{tourId}/{saleId}", method=RequestMethod.POST,produces = "text/html")
   public String createTourSaleComment(@PathVariable("tourId") long tourId, @PathVariable("saleId") long saleId, @Valid Comment comment, BindingResult bindingResult,Model uiModel,  HttpServletRequest httpServletRequest) throws Exception {
      if (bindingResult.hasErrors()){
         uiModel.addAttribute("status", "FAIL");
         return "comment/status";
      }
      comment.setId(null);
      comment.setSaleId(saleId);
      comment.setTourId(tourId);
      comment.setCreatedDate(new Date(System.currentTimeMillis()));
      if (UserUtil.hasLogined()){
         comment.setCreatedUser(UserUtil.getAuthenticated().getUsername());
         Participant participant = this.participantService.findParticipant(UserUtil.getAuthenticated().getParticipantId());
         comment.setParticipant(participant);
      }
      commentService.save(comment);
      uiModel.addAttribute("status", "SUCC");
      return "comment/status";
   }
   
   @RequestMapping(value = "/{commentId}", method=RequestMethod.DELETE)
   public String deleteSightSeeing(@PathVariable("commentId") Long commentId, Model uiModel) {
      this.commentService.delete(commentId);
      return "redirect:/comment/success";
   }
   
   @RequestMapping(value = "/success", method=RequestMethod.GET)
   public String viewSuccessfulStatus(Model uiModel) {
      uiModel.addAttribute("status", "SUCC");
      return "comment/status";
   }

   private String createCateloguesType(String attractionType, String attractionMenu){
      return attractionType+"_"+attractionMenu;
   }
}
